package com.tianyang.yukuaifuzs.business.main.view;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.main.adapter.MerchantManageAdapter;
import com.tianyang.yukuaifuzs.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifuzs.business.main.contract.MerchantManageContract;
import com.tianyang.yukuaifuzs.business.main.presenter.MerchantManagePresenter;
import com.tianyang.yukuaifuzs.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.common.utils.CommonUtils;
import com.tianyang.yukuaifuzs.module.dialog.MerchantFilterDialog;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.MerchantFilterListener;
import com.tianyang.yukuaifuzs.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifuzs.utils.SoftInputUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Description：商户管理
 * Created by xyj on 2024/3/10
 */
public class MerchantManageActivity extends BaseTitleActivity implements MerchantManageContract.View {

    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;
    @BindView(R.id.emptyView)
    RelativeLayout emptyView;
    @BindView(R.id.filterIv)
    ImageView filterIv;

    private MerchantFilterDialog vMerchantFilterDialog;

    private MerchantManageAdapter vMerchantManageAdapter;

    private MerchantManageContract.Presenter mPresenter;

    private int nowPage = CommonConfig.PAGE_NUM;

    private int posStatus = -1, merchantType = -1;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_merchant_manage;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("商户管理");
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vMerchantManageAdapter = new MerchantManageAdapter();
        vRecyclerview.setAdapter(vMerchantManageAdapter);
        vMerchantManageAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vMerchantManageAdapter.setPreLoadNumber(2);

        mPresenter = new MerchantManagePresenter(this);
        mPresenter.getMerchantManageList(nowPage, phoneNumberEt.getText().toString(), posStatus, merchantType);
    }

    @Override
    protected void setListener() {
        super.setListener();
        filterIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vMerchantFilterDialog == null) {
                    vMerchantFilterDialog = new MerchantFilterDialog(mContext);
                    vMerchantFilterDialog.setListener(new MerchantFilterListener() {
                        @Override
                        public void callback(int mchTypeFilter, int authStatusFilter) {
                            posStatus = authStatusFilter;
                            merchantType = mchTypeFilter;
                        }
                    });
                }
                vMerchantFilterDialog.show();
            }
        });
        phoneNumberEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    SoftInputUtil.hideSoftInput(MerchantManageActivity.this);
                    nowPage = CommonConfig.PAGE_NUM;
                    mPresenter.getMerchantManageList(nowPage, phoneNumberEt.getText().toString(), posStatus, merchantType);
                    vMerchantManageAdapter.setEnableLoadMore(true);
                }
                return false;
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.getMerchantManageList(nowPage, phoneNumberEt.getText().toString(), posStatus, merchantType);
                vMerchantManageAdapter.setEnableLoadMore(true);
            }
        });
        vMerchantManageAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getMerchantManageList(nowPage, phoneNumberEt.getText().toString(), posStatus, merchantType);
            }
        }, vRecyclerview);
        vMerchantManageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PosUserOutput output = ((MerchantManageAdapter) adapter).getItem(position);
                if (output != null) {
                    switch (view.getId()) {
                        case R.id.copyPtRtv:
                            CommonUtils.copyText(mContext, output.getBangdingcommercode());
                            break;
                        case R.id.copyMchRtv:
                            CommonUtils.copyText(mContext, output.getMchId());
                            break;
                        case R.id.copyPhoneRtv:
                            CommonUtils.copyText(mContext, output.getPhone());
                            break;
                    }
                }
            }
        });
        vMerchantManageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PosUserOutput output = ((MerchantManageAdapter) adapter).getItem(position);
                if (output != null && output.getPosStatus() >= 3) {
                    MainOpenProvider.getInstance().openMerchantDetailActivity(mContext, output);
                }
            }
        });
    }

    @Override
    public void showMerchantManageListView(List<PosUserOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vMerchantManageAdapter.setNewData(outputs);
            } else {
                vMerchantManageAdapter.addData(outputs);
            }
            nowPage++;
            vRecyclerview.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vRecyclerview.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vMerchantManageAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vMerchantManageAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vMerchantManageAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MerchantManageContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
        if (vMerchantFilterDialog != null && vMerchantFilterDialog.isShowing()) {
            vMerchantFilterDialog.dismiss();
            vMerchantFilterDialog = null;
        }
     }
}
